package com.ATMgui;

import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Zhuanzhang extends JPanel implements ActionListener {
    private Icon sureBtnImg = Util.getIcon("/image/buttonSure.jpg");
    private JTextField jTextField;
    private JTextField jTextField1;
    private JButton sure;
    private ATM frame;
    private int index = -1;

    public Zhuanzhang(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        JLabel textlabel1 = new JLabel("转账账户：");
        textlabel1.setBounds(150, 150, 100, 30);
        this.add(textlabel1);

        //转账账户
        this.setOpaque(false);
        jTextField = new JTextField();
        jTextField.setBounds(250, 150, 250, 30);
        this.add(jTextField);

        JLabel textlabel2 = new JLabel("转账金额：");
        textlabel2.setBounds(150, 200, 100, 30);
        this.add(textlabel2);

        //转账金额
        this.setOpaque(false);
        jTextField1 = new JTextField();
        jTextField1.setBounds(250, 200, 250, 30);
        this.add(jTextField1);


        this.setOpaque(false);
        sure = new JButton(sureBtnImg);
        sure.setBounds(200, 320, 100, 20);
        this.add(sure);
        sure.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        boolean flag = false;
        if (e.getSource() == sure) {
            for (int i = 0; i < frame.getUsersList().size(); i++) {
                if (jTextField.getText().equals(frame.getUsersList().get(i).getUserName())) {
                    flag = true;
                    index = i;
                    if (jTextField.getText().equals(frame.getUsersList().get(frame.getNum()).getUserName())) {
                        JOptionPane.showMessageDialog(this, "不能给自己转账");
                        break;
                    } else {
                        if (Double.valueOf(jTextField1.getText()) > frame.getUsersList().get(frame.getNum()).getBlance()) {
                            JOptionPane.showMessageDialog(this, "当前余额不足");
                            break;
                        } else {
                            double money1 = frame.getUsersList().get(index).getBlance() + Double.valueOf(jTextField1.getText());
                            double money2 = frame.getUsersList().get(frame.getNum()).getBlance() - Double.valueOf(jTextField1.getText());
                            frame.getUsersList().get(frame.getNum()).setBlance(money2);
                            frame.getUsersList().get(index).setBlance(money1);
                            frame.switchPanel("菜单");
                            break;
                        }

                    }
                }
            }
            if (!flag) {
                JOptionPane.showMessageDialog(this, "查无此人");
            }
        }
    }
}
